package com.dongbao.core.rules.java;

import com.dongbao.core.base.CodeRulesCommonBase;
import com.dongbao.core.rules.CodeRulesBase;
import com.dongbao.entity.AwardedInfo;
import com.intellij.lang.jvm.JvmParameter;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.impl.source.PsiClassImpl;

import java.util.List;

/**
 *  方法参数最多不能超过5个，超过考虑用对象，方法体内共用变量超过6个,必须封装成Dto或者简化入参参数
 * */
public class MethodParamRules extends CodeRulesCommonBase implements CodeRulesBase {

    public MethodParamRules(VirtualFile afterFile, PsiFile psiFile, Project project) {
        super(afterFile,psiFile,project);
        this.code = "A007";
        this.level = "A";
    }

    /**
     * 执行规则判断
     * */
    @Override
    public void run(List<AwardedInfo> deduction) {
        /* 执行分析当前class是否负责当前规则 */
        PsiAnnotation[] annotations = psiClassImpls.get(0).getModifierList().getAnnotations();
        for(PsiClassImpl psiClass:psiClassImpls){
            /* 获取所有方法 */
            PsiMethod[] allMethods = psiClass.getMethods();
            for(PsiMethod method:allMethods){
                JvmParameter[] parameters = method.getParameters();
                if(parameters!=null && parameters.length>5){

                    /* 获取错误代码行位置 */
                    Document document = PsiDocumentManager.getInstance(psiClass.getProject()).getDocument(psiClass.getContainingFile());
                    int lineNumber = document.getLineNumber(method.getTextOffset());

                    packDeduction(
                            deduction,
                            psiClass.getName(),
                            psiPackageStatements.get(0).getPackageName(),
                            method.getName(),
                            "方法参数最多不能超过5个，超过考虑用对象，方法体内共用变量超过6个,必须封装成Dto或者简化入参参数",lineNumber,lineNumber);
                }
            }
        }
    }

    @Override
    public String describe() {
        return "设计模式与解耦";
    }
}
